#include "head.h"
#include "ui_head.h"

#include <QMainWindow>

Head::Head(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Head)
{
    ui->setupUi(this);

}

Head::~Head()
{
    delete ui;
}

void Head::on_pushButton_clicked()
{
    getMainWin()->showMinimized();
}


void Head::on_pushButton_2_clicked()
{
    getMainWin()->close();
}

QMainWindow *Head::getMainWin()
{

    static QMainWindow* win = nullptr;

    if(win){
        return win;
    }

    QObject* obj = nullptr;
    obj = this->parent();

    while(obj){
        win = qobject_cast<QMainWindow*>(obj->parent());
        if(win){
            return win;
        }else{
            obj = obj->parent();
        }
    }
    return nullptr;
}

void Head::mouseMoveEvent(QMouseEvent *event)
{


    if(event->buttons() == Qt::LeftButton){  //如果这里写这行代码，拖动会有点问题
        if(mouse_press){
            getMainWin()->move(event->globalPos() - mousePoint);
            event->accept();
        }
    }
}

void Head::mousePressEvent(QMouseEvent *event)
{

    if( (event->button() == Qt::LeftButton) ){
            mouse_press = true;
            mousePoint = event->globalPos() - getMainWin()->pos();
    //        event->accept();
        }
        else if(event->button() == Qt::RightButton){
            //如果是右键
//            this->close();

        }


}

void Head::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton){
        mouse_press = false;
    }
}

